<?php
namespace Home\Controller;
use Think\Controller;
class InfoCenterController extends Controller {
    public function index(){
        $pname = I("get.cname");
        if($pname){
            $where['pname'] = $pname;
        }else{
            $url = $_SERVER['PHP_SELF'];
            $categorys = substr(strstr($url,'php/'),4); //URL不带入口名称
            $category = strstr($categorys,'/',true);    //控制器名称
            $id = M("category") -> where(array("pname" => $category)) -> getField("id");
            $where["pname"] = M("category") -> where(array("pid" => $id)) -> limit(1) -> getField("pname");
        }
        $category = M("category") -> where($where) -> select();
        $pid = $category[0]['id'];
        $count = M("article") -> where(array("pid" => $pid)) -> count();
        $page = new \Think\Page($count,15);
        $page -> setConfig('header',"<div class='subPage'>共%TOTAL_ROW%条</div>");
        $page -> setConfig('first','首页');
        $page -> setConfig('prev','上一页');
        $page -> setConfig('next','下一页');
        $page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
        $page -> setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page -> show();
        $article = M("article") ->field("id,title,time") -> where(array("pid" => $pid)) ->limit($page -> firstRow.','.$page -> listRows) -> order("time desc") -> select();
        $this -> assign("category",$category[0]);
        $this -> assign('article',$article);
        $this -> assign('page',$show);
        $this->display();
    }

    public function detail(){
        $id = I('id');
        $firstId = $id - 1;
        if(M('article') -> where("id = {$firstId}") -> select()){
            $firstArticle = M('article') -> where("id = {$firstId}") -> select();
            $this -> assign('firstArticle',$firstArticle[0]);
        }else{
            $firstArticle['title'] = '没有了';
            $this -> assign('firstArticle',$firstArticle);
        }
        $lastId = $id + 1;
        if(M('article') -> where("id = {$lastId}") -> select()){
            $lastArticle = M('article') -> where("id = {$lastId}") -> select();
            $this -> assign('lastArticle',$lastArticle[0]);
        }else{
            $lastArticle['title'] = '没有了';
            $this -> assign('lastArticle',$lastArticle);
        }
        $article = M('article') -> where("id = {$id}") -> select();
        $pid = M("article") -> where(array("id" => $id)) -> getField("pid");
        $category=M("category") ->field("pid,catname,url") -> where(array("id" => $pid)) -> select();
        $parent_category = M("category") ->field("catname,url") -> where(array("id" => $category[0]['pid'])) -> select();
        $this -> assign("parent_category",$parent_category[0]);
        $this -> assign("category",$category[0]);
        $this -> assign('article',$article[0]);
        $this->display();
    }
}